
var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            searchName: null,
            sidx: null,
            order: null
        },
        page:{total:0,limit:30,page:1},
        dataList:[],
        isadd: false,
		showList: true,
		showView: false,
		title: null,
        addFlag: false,
        editFlag: false,
		fsmsTemplateCanvas: {},

        columnsList: [
          {name: '模板ID', field: 'canvasId' , isShow: true},
          {name: '区域类别', field: 'canvasType' , isShow: true},
          {name: '排序', field: 'canvasSort' , isShow: true},
          {name: '内容', field: 'content' , isShow: true},
          {name: '内容标识', field: 'contentFlag' , isShow: true},
        ],
        contentFlagList: [
        {'value': 1, 'text': '模板提供'},
        {'value': 2, 'text': '发送时提供'}
        ],
        contentFlagMap: {},
        contentFlagSearchList: {},
		rules: {
          canvasId: [
            {required: true, message: '请输入模板ID', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
          test:[]
       }
	},
    mounted: function () {
        this.contentFlagMap = list2Map(this.contentFlagList)
        this.getDataList();
    },
	methods: {
		query: function () {
			vm.reload();
		},
        getDataList:function () {
            this.q.page = this.page.page;
            this.q.limit = this.page.limit;

            $.ajax({
                type: "POST",
                url: "../fsmstemplatecanvas/list",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        //debugger;
                        vm.dataList = r.data.list;
                        vm.page.total = r.data.totalCount;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
		add: function(){
		    vm.isadd = true; 
			vm.showList = false;
			vm.showView = false;
			vm.title = "新增";
			vm.fsmsTemplateCanvas = {};
		},
		update: function (canvasId) {
			if(canvasId == null){
				return ;
			}
			vm.showList = false;
			vm.showView = false;
            vm.title = "修改";
            vm.getInfo(canvasId)
		},
        view2update: function () {
            vm.showList = false;
            vm.showView = false;
            vm.title = "修改";
        },
		view: function(canvasId){
        		vm.title = "查看";
	        vm.getInfo(canvasId)
	        	vm.showList = false;
	        	vm.showView = true;
        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../fsmstemplatecanvas/save" : "../fsmstemplatecanvas/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.fsmsTemplateCanvas),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['fsmsTemplateCanvasForm'].validate(vm.saveOrUpadateValid);
		},
		del: function (canvasIds) {

			if(canvasIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../fsmstemplatecanvas/delete",
				    data: JSON.stringify(canvasIds),
				    success: function(r){
						if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		delOne: function (canvasId) {

            var canvasIds = [];
            canvasIds[0] = canvasId;

        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "GET",
        			url: "../fsmstemplatecanvas/delete/"+canvasId,
        			success: function(r){
                        if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
        			}
        		});
        	});
        },
		getInfo: function(canvasId){
			$.get("../fsmstemplatecanvas/info/"+canvasId, function(r){
                vm.fsmsTemplateCanvas = r.data;
            });
		},
        handleCommand:function (commond) {
            if(commond=='d')
            {
                location.href = "../fsmstemplatecanvas/download?query=" + escape(JSON.stringify(vm.q));
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(commond=='a')
            {
                $("#data").tableExport({type:'csv'},'data');
                return;
            }

            if(commond=='b')
            {
                $("#data").tableExport({type:'txt'},'data');
                return;
            }

            if(commond=='c')
            {
                $("#data").tableExport({type:'excel',escape:false,
                    mso: {fileFormat:'xlsx',
                        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                        worksheetName: ['Data']}},'data');
                return;
            }
        },
        exportExl: function () {
            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            $("#data").tableExport({type:'excel',escape:false,
                mso: {fileFormat:'xlsx',
                    excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                    worksheetName: ['Data']}},'data');

        },

        pageSizeChange:function (pageSize) {
            this.page.limit = pageSize;
            this.reload();
        },
        pageCurrentChange:function (page) {
            this.page.page = page;
            this.reload();
        },
        pageChange:function () {
            this.reload();
        },
		reload: function (event) {
			this.isadd = false;
			this.showList = true;
            this.getDataList();
		}
	}
});
